package com.cash.app.custom

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.TextView
import androidx.annotation.StyleRes
import com.cash.app.R
import com.cash.app.utils.hide
import com.cash.app.utils.visible

/**
 * A flexible dialog utility class that supports customization of:
 * - Title visibility and content
 * - Custom content view
 * - Bottom buttons (quantity and visibility)
 * - Cancellation behavior
 */
class CommonDialog private constructor(
    private val context: Context, private val builder: Builder
) {
    private var dialog: Dialog? = null
    private var contentContainer: ViewGroup? = null
    private var titleView: TextView? = null
    private var buttonContainer: ViewGroup? = null
    private var buttonLine: View? = null

    init {
        initDialog()
    }

    private fun initDialog() {
        dialog = Dialog(context).apply {
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setCancelable(builder.cancelable)
            setCanceledOnTouchOutside(builder.cancelableOnTouchOutside)
            setOnDismissListener(builder.dismissListener)
            setContentView(R.layout.dialog_common)

            // Initialize views
            titleView = findViewById(R.id.dialogTitle)
            contentContainer = findViewById(R.id.dialogContent)
            buttonContainer = findViewById(R.id.dialogButtonContainer)
            buttonLine = findViewById(R.id.dialog_button_line)

            // Setup title
            titleView?.apply {
                text = builder.title
                visibility = if (builder.showTitle) View.VISIBLE else View.GONE
            }

            // Setup content
            builder.contentView?.let {
                contentContainer?.removeAllViews()
                contentContainer?.addView(it)
            }

            // Setup buttons
            setupButtons()
        }
    }

    private fun setupButtons() {
        buttonContainer?.removeAllViews()

        if (!builder.showButtons) {
            buttonContainer?.visibility = View.GONE
            buttonLine?.visibility = View.GONE
            return
        }

        builder.buttons.forEachIndexed { index, buttonConfig ->
            val root = LayoutInflater.from(context).inflate(
                R.layout.dialog_button, buttonContainer, false
            )
            val button = root.findViewById<TextView>(R.id.button_tv)
            val line = root.findViewById<View>(R.id.button_line)

            button.setTextColor(context.resources.getColor(buttonConfig.textColor))

            button.apply {
                text = buttonConfig.text
                setOnClickListener {
                    buttonConfig.onClick?.invoke(this@CommonDialog)
                }
            }
            if (index == builder.buttons.size - 1) {
                line.hide()
            } else {
                line.visible()
            }
            buttonContainer?.addView(root)
        }

        buttonContainer?.visibility = if (builder.buttons.isEmpty()) View.GONE else View.VISIBLE
    }

    fun show() {
        dialog?.show()
    }

    fun dismiss() {
        dialog?.dismiss()
    }

    class Builder(private val context: Context) {
        var title: String = ""
        var showTitle: Boolean = true
        var contentView: View? = null
        var showButtons: Boolean = true
        var cancelable: Boolean = true
        var cancelableOnTouchOutside: Boolean = true
        var dismissListener: DialogInterface.OnDismissListener? = null
        var buttons: MutableList<ButtonConfig> = mutableListOf()

        @StyleRes
        var themeResId: Int = R.style.DialogTheme

        fun setTitle(title: String) = apply {
            this.title = title
        }

        fun showTitle(show: Boolean) = apply {
            this.showTitle = show
        }

        fun setContentView(view: View) = apply {
            this.contentView = view
        }

        fun showButtons(show: Boolean) = apply {
            this.showButtons = show
        }

        fun setCancelable(cancelable: Boolean) = apply {
            this.cancelable = cancelable
        }

        fun setCancelableOnTouchOutside(cancelable: Boolean) = apply {
            this.cancelableOnTouchOutside = cancelable
        }

        fun setOnDismissListener(listener: DialogInterface.OnDismissListener) = apply {
            this.dismissListener = listener
        }

        fun addButton(
            text: String,
            onClick: ((CommonDialog) -> Unit)? = null,
            textColor: Int = R.color.black_1
        ) = apply {
            buttons.add(ButtonConfig(text, onClick, textColor))
        }

        fun build(): CommonDialog {
            return CommonDialog(context, this)
        }
    }

    data class ButtonConfig(
        val text: String,
        val onClick: ((CommonDialog) -> Unit)? = null,
        val textColor: Int = R.color.black_1
    )
} 